package server.processors;

import java.nio.channels.ClosedChannelException;
import java.util.List;

import server.NetworkMediator;
import server.ServerStateMediator;
import common.Round;
import common.RoundList;
import common.Sendable;
import common.RoundRequest;

public class RoundRequestProcessor extends Processor  {
	
	public RoundRequestProcessor(){}
	
	public RoundRequestProcessor(NetworkMediator mediator, ServerStateMediator server ) {
		super(mediator, server);
	}

	@Override
	public boolean isInterestedIn(Sendable s) {
		return s instanceof RoundRequest;
	}

	public void run() {
		
		NetworkMediator network = getNetwork();
		ServerStateMediator server = getServer();
		
		List<Round> rounds = server.getServerState().getAvailableRounds();
		
		RoundList list = new RoundList();
		list.addAll( rounds );
		
		try {
			//The Client does not have a player object instantiated at this point most likely.
			network.sendTo( server.getClient(), list );
		} catch (ClosedChannelException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Processor getInstanceOf(NetworkMediator mediator, ServerStateMediator server) {
		return new RoundRequestProcessor( mediator, server );
	}

}
